package com.yc.forum.servlet;



import com.yc.forum.dao.ProblemDao;
import com.yc.forum.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ClassifyQueryProblemServlet",value = "/ClassifyQueryProblem.s")
public class ClassifyQueryProblemServlet extends HttpServlet {
    private ProblemDao problemDao=new ProblemDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String module=request.getParameter("module");
        String page=request.getParameter("page");
        String size=request.getParameter("size");
        String search=request.getParameter("search");
        Map<String,Object> pagelist= problemDao.selectClassify(module,page,size,search);
        //去除标签
        List<Map<String,String>> body=(List<Map<String,String>>) pagelist.get("list");
        System.out.println("======================="+pagelist.get("list"));
        List<Map<String,String>> newbody=new ArrayList<>();
        System.out.println(body.size());
        for(int i=0;i<body.size();i++){
            Map<String,String> map=body.get(i);
            String _text=map.get("body");
            _text=Utils.takeOutHtmlTab(_text);
            System.out.println("问题正文长度：===="+_text.length());
            String text;
            if(_text.length()>300){
                text=_text.substring(0,300);
            }else {
                text=_text;
            }
            map.put("text", text);
            newbody.add(map);
        }
        pagelist.put("newbody",newbody);
        System.out.println("pagelist="+pagelist.toString());
        Utils.toJson(pagelist,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }


}
